﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightGolf.Game;
using SilverlightGolf.Core;

namespace SilverlightGolf.Client
{
    public partial class HolePres : UserControl
    {
        public Hole GolfHole { get; private set; }
        public IList<PlayersBallPres> PlayersBalls { get; private set; }
        
        public IList<IDrawable> DrawableObjects { get; private set; }

        public HolePres(Hole hole)
        {
            InitializeComponent();
            GolfHole = hole;
            DrawableObjects = new List<IDrawable>();
            PlayersBalls = new List<PlayersBallPres>();
            hole.StateChanged += new EventHandler<HoleStateEventArgs>(HoleStateChanged);

            hole.GameObjects.ItemAdded += new EventHandler<ListItemEventArgs<IGameObject>>(GameObjectAdded);

            hole.Initialize();
        }

        public void Draw()
        {
            foreach (var obj in DrawableObjects)
            {
                obj.Draw();
            }
        }

        void GameObjectAdded(object sender, ListItemEventArgs<IGameObject> e)
        {
            if(e.Item is PlayersBall)
            {
                var ball = new PlayersBallPres((PlayersBall)e.Item);
                this.HoleCanvas.Children.Add(ball);
                DrawableObjects.Add(ball);
                PlayersBalls.Add(ball);
            }
        }

        void HoleStateChanged(object sender, HoleStateEventArgs e)
        {
            if (e.State is ReadyForShot)
            {

            }
        }
    }
}
